﻿using ProjBackEnd.DAL;
using ProjBackEnd.Lib;
using ProjBackEnd.Areas.Sys.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace ProjBackEnd.Areas.Sys.Controllers
{
    public class DeptInfoController : ApiController
    {
        /// <summary>
        /// 获取部门信息 eg. :0000000001|WEB|COM1
        /// </summary>
        /// <param name="key">用户id|平台cd|公司代码</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/sys/comdept")]
        public RespSysDeptInfo GetDeptList(string key)
        {
            RespSysDeptInfo resp = new RespSysDeptInfo();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }
            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 3)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strPlatcd = strsKeyOriginal[1];
            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }
            string strComcd = strsKeyOriginal[2];

            resp = basecodeLogic.GetDeptList(strComcd);
            return resp;
        }

        /// <summary>
        /// 操作部门信息
        /// </summary>
        /// <param name="_reqInfo">操作请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/sys/comdept")]
        public RespSysNullResultInfo UpdateDeptInfo(ReqDept _reqInfo)
        {
            RespSysNullResultInfo resp = new RespSysNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = basecodeLogic.UpdateDept(_reqInfo.deptinfo);
            return resp;
        }

    }
}
